---@diagnostic disable: undefined-global


local function dig_my()   --dig
	turtle.dig()
	turtle.forward()
	if turtle.detectUp() then
		turtle.digUp()
	end
	if turtle.detectDown() then
		turtle.digDown()
	end
end

local function dig_upanddown()   --dig_upanddown
	if turtle.detectUp() then
		turtle.digUp()
	end

	if turtle.detectDown() then
		turtle.digDown()
	end
end
	

local function turtle_fuel()   --turtle_fuel
	print("fuel_level = "..turtle.getFuelLevel().."\n")
	print("fuel_limit = "..turtle.getFuelLimit().."\n")
	if turtle.getFuelLevel()<=12 then
		turtle.refuel(1)
	end
	
end


local counter = 0
local function turtle_init()    --turtle_init
	turtle.refuel(1)
end

local function turtle_loop()     --turtle_loop
	while true do 
		turtle_fuel()
		while turtle.detect() do
			print("["..counter.."] OK!\n")
			if turtle.getFuelLevel() <=12 then
				turtle.refuel(1)
			end
			counter = counter +1
			dig_my()        --dig forward
	
			turtle.turnLeft()
			if turtle.detect() then
				dig_my()   --dig left
				turtle.turnRight()
				turtle.turnRight()
				turtle.forward()
			else
				turtle.forward()
				dig_upanddown()
				turtle.turnRight()
				turtle.turnRight()
				turtle.forward()
			end
	
	
			if turtle.detect() then
				dig_my()   --dig right
				turtle.turnLeft()
				turtle.turnLeft()
				turtle.forward()
				turtle.turnRight()
			else
				turtle.forward()
				dig_upanddown()
				turtle.turnLeft()
				turtle.turnLeft()
				turtle.forward()
				turtle.turnRight()
			end
		end
		if not turtle.detect() then
		turtle.forward()
		end
	end
end 








turtle_loop()
turtle_init()